package apj.demo.message.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import apj.demo.model.Department;
import apj.demo.util.JDBCUtil;

@WebServlet("/work")
public class PageWorkServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String wortType = request.getParameter("workType");
		boolean ready = false;
		List<String> dep_namelList = new ArrayList<String>();
		
		try{
			if(wortType.equals("修改")){
				ready = changeMSG(request);
			}else if (wortType.equals("添加")) {
				ready = addMSG(request);
			}else if(wortType.equals("显示")){
				dep_namelList = showMSG(request);
				if(dep_namelList.size() != 0){
					ready = true;
				}
			}else if(wortType.equals("保存")){
				ready = saveMSG(request);
			}else{
				System.out.println("没进去");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		if(ready == true){
			com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
			json.put("ready", ready);
			json.put("dep_namelLists", dep_namelList);
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();
			
		}
		
	}
	
	public boolean changeMSG(HttpServletRequest request) throws Exception{
		boolean ready = true;
		
		String type = request.getParameter("type");
		String[] type_nameSTR = request.getParameterValues("type_name");
		List<String> type_name = Arrays.asList(type_nameSTR);
		String[] type_name_contentsSTR = request.getParameterValues("type_name_contents");
		List<String> type_name_contents = Arrays.asList(type_name_contentsSTR);
		
		Connection conn = null;
		PreparedStatement prep = null;
		
		for(int i=1; i<type_name.size(); i++){
			if(type_name_contents.get(i) == ""){
				continue;
			}
			
			String sql = "update " + type + " set " + type_name.get(i) + " = '" + type_name_contents.get(i) + "' where " + type_name.get(0) + " = " + type_name_contents.get(0);
			
			try {
				conn = JDBCUtil.getConnection();
				prep = conn.prepareStatement(sql);
				int num  = prep.executeUpdate(sql);
				if(num == 0){
					ready = false;
					break;
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				conn.close();
				prep.close();
			}
			
		}
		
		return ready;
	}
	
	public boolean addMSG(HttpServletRequest request) throws Exception{
		boolean ready = true;
		
		String type = request.getParameter("type");
		String[] type_nameSTR = request.getParameterValues("type_name");
		List<String> type_name = Arrays.asList(type_nameSTR);
		String[] type_name_contentsSTR = request.getParameterValues("type_name_contents");
		List<String> type_name_contents = Arrays.asList(type_name_contentsSTR);
		
		Connection conn = null;
		PreparedStatement prep = null;
		
			String sql = "insert into "+type+"("+type_name.get(0)+") values ("+type_name_contents.get(0)+")";
			
			try {
				conn = JDBCUtil.getConnection();
				prep = conn.prepareStatement(sql);
				int num  = prep.executeUpdate(sql);
				if(num == 0){
					ready = false;
				}else{
					changeMSG(request);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				conn.close();
				prep.close();
			}
			
		return ready;
	}
	
	public List<String> showMSG(HttpServletRequest request) throws Exception {
		
		String sql = "select vendor from vendor";
		
		Connection conn = null;
		PreparedStatement prep = null;
		ResultSet rst = null;
		List<String> dep_names = new ArrayList<String>();
		
		try{
			conn = JDBCUtil.getConnection();
			prep = conn.prepareStatement(sql);
			rst = prep.executeQuery();
			
			while(rst.next()){
				String n = "";
				n = rst.getString("vendor");
				dep_names.add(n);
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			conn.close();
			prep.close();
			rst.close();
		}
		
		return dep_names;
	}
	
	public boolean saveMSG(HttpServletRequest request) throws Exception{
		boolean ready = false;
		
		String searchType = request.getParameter("search_type");
		String[] listStrings = request.getParameterValues("string");
		List<String> list = Arrays.asList(listStrings);
		
		for(int i=0; i<list.size(); i++){
			if(list.get(i) == ""){
				continue;
			}
			
			Connection conn = null;
			PreparedStatement prep = null;
			ResultSet rst = null;
			int saveID = 0;
			int saveNum = 0;
			
			String sql0 = "select bill_id from (select * from bill) as b where vendor = '"+searchType+"' and bill_finish = 'F' limit 0,1";
			
			try{
				conn = JDBCUtil.getConnection();
				prep = conn.prepareStatement(sql0);
				rst = prep.executeQuery();
				if(rst.next()){
					saveID = rst.getInt("bill_id");
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			
			String sql = "update bill set pay_type = '"+list.get(i)+"' where bill_id = " + saveID;
			String sql2 = "update bill set bill_finish = 'T' where bill_id = " + saveID;
			
			try{
				prep = conn.prepareStatement(sql);
				saveNum = prep.executeUpdate(sql);
				
				if(saveNum == 1){
					prep = conn.prepareStatement(sql2);
					int lNume = prep.executeUpdate(sql2);
					if (lNume == 1) {
						ready = true;
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				conn.close();
				prep.close();
				rst.close();
			}
		}
		
		return ready;
	}

}
